% Matlab code to visualize the equilibrium and illustrate the spirals

% To run this file seperately, specify the root directly below:
% cd 'C:/Users/Jilun/Dropbox/WorldEconomy_RiskIntolerance/paper RFS final submission/replication/numerical and calibration exercises'


% Make the figures for the extension with LSAPs
%%% Make two figures
%%% Figure 4: Unique equilbirium with LSAPs
%%% Figure 5: Multiple equilibria with LSAPs

clear all;
global g rho sigma PStar taub tauh k l tau tauBarBench
global z0_2
loadVariables; % Sets the above parameters and the baseline shocks

% Take the second shock and solve with lsap
etag = 0.1; % Take gov's tax capacity 1/3 of the economy
wg = 2; % Government takes some risk out of the market
global lsap;
lsap = etag*(wg-1);

eq = solveEquilibrium(z0_2,0);
eq_g = solveEquilibrium(z0_2,lsap);


%%%%%%%%%%%% FIGURE 4
%%%%%%%%%%%% Case with unique equilibrium
% Now plot the case without and with LSAPs
figure('Position',[30 30 830 430]);
pArr = eq.pArr;
sharpeActualArr = eq.sharpeActualArr;
sharpeRequiredArr = eq.sharpeRequiredArr;
ind = eq.ind;

pArr_g = eq_g.pArr;
sharpeActualArr_g = eq_g.sharpeActualArr;
sharpeRequiredArr_g = eq_g.sharpeRequiredArr;
ind_g = eq_g.ind;
ph_g = eq_g.ph;
% Now plot the equilibrium curves
hold on;
h(1) = plot(pArr,sharpeActualArr,'b','LineWidth',3);
h(2) = plot(pArr,sharpeRequiredArr,'r--','LineWidth',3);
% Find and mark the equilibrium
plot(pArr(ind),sharpeActualArr(ind),'ko-','LineWidth',3);

h(3) = plot(pArr_g,sharpeRequiredArr_g,'r','LineWidth',3);
% Find and mark the equilibrium
indEq = find(sharpeActualArr_g - sharpeRequiredArr_g<0,1,'first');
plot(pArr_g(ind_g),sharpeActualArr_g(ind_g),'ko-','LineWidth',3);

% Draw a line to mark the higher level of asset prices with LSAPs
line([ph_g ph_g],[sharpeActualArr_g(end) sharpeRequiredArr_g(1)],'LineStyle','-','LineWidth',1);

% Mark ph on the figure
specText = sprintf('$p^h(\\lambda)$');
text(ph_g,sharpeActualArr_g(end)+0.01,specText,'Interpreter','Latex','FontSize',20);

titleStr = sprintf('Large-scale asset purchases with constrained interest rate');
title(titleStr,'Interpreter','Latex','FontSize',20);
xlabel('Normalized price, $p_0$','Interpreter','Latex','FontSize',20);

legendStr1 = sprintf('Actual Sharpe ratio, $r^f_0=0$');
legendStr2 = sprintf('Required Sharpe ratio without LSAPs');
legendStr3 = sprintf('Required Sharpe ratio with LSAPs, $\\lambda=%1.1f$',lsap);
hL = legend(h([1 3 2]),legendStr1,legendStr3,legendStr2);
set(hL,'orientation','vertical','fontsize',15,'interpreter','latex');
xlim([ph 1]);
ylim([sharpeActualArr(end) sharpeActualArr(1)]);
saveas(gcf,[pwd '/figures/fig4.eps'],'epsc');


%%%%%%%%%%%% FIGURE 5
%%%%%%%%%%%% Case with multiple equilibrium

global g rho sigma PStar taub tauh k l tau
global z0_M
loadVariables_M; % Resets the above parameters and loads the shock that leads to multiplicity

% Calculate the equilibrium. Don't change lsap: same as the one set before
global lsap
eq = solveEquilibrium(z0_M,0); % Solve with the same lsap as above
eq_g = solveEquilibrium(z0_M,lsap); % Solve with the same lsap as above

pArr = eq.pArr;
sharpeActualArr = eq.sharpeActualArr;
sharpeRequiredArr = eq.sharpeRequiredArr;
ind = eq.ind;

pArr_g = eq_g.pArr;
sharpeActualArr_g = eq_g.sharpeActualArr;
sharpeRequiredArr_g = eq_g.sharpeRequiredArr;
ind_g = eq_g.ind;
ph_g = eq_g.ph;

% Now plot the case without LSAPs and multiplicity and with LSAPs
figure('Position',[30 30 830 430]);

% Now plot the equilibrium curves
hold on;
h(1) = plot(pArr,sharpeActualArr,'b','LineWidth',3);

h(2) = plot(pArr,sharpeRequiredArr,'r--','LineWidth',3);
% Find and mark the equilibria
indEq_1 = 1;
plot(pArr(indEq_1),sharpeActualArr(indEq_1),'ko-','LineWidth',3);
specText = sprintf('$L$');
text(pArr(indEq_1),sharpeActualArr(indEq_1)+0.007,specText,'Interpreter','Latex','FontSize',20);

indEq_3 = find(sharpeActualArr - sharpeRequiredArr>0,1,'last');
plot(pArr(indEq_3),sharpeRequiredArr(indEq_3),'ko-','LineWidth',3);
specText = sprintf('$H$');
text(pArr(indEq_3),sharpeRequiredArr(indEq_3)+0.005,specText,'Interpreter','Latex','FontSize',20);

h(3) = plot(pArr_g,sharpeRequiredArr_g,'r','LineWidth',3);
% Find and mark the equilibria
indEq_3 = find(sharpeActualArr_g - sharpeRequiredArr_g>0,1,'last');
plot(pArr_g(indEq_3),sharpeRequiredArr_g(indEq_3),'ko-','LineWidth',3);
specText = sprintf('$H''$');
text(pArr_g(indEq_3),sharpeRequiredArr_g(indEq_3)+0.004,specText,'Interpreter','Latex','FontSize',20);


% Draw a line to mark the higher level of h-only asset prices with LSAPs
line([ph_g ph_g],[sharpeRequiredArr_g(end) sharpeRequiredArr_g(1)],'LineStyle','-','LineWidth',1);

% Mark ph on the figure
specText = sprintf('$p^h(\\lambda)$');
text(ph_g,sharpeRequiredArr_g(end)+0.01,specText,'Interpreter','Latex','FontSize',20);


titleStr = sprintf('Large-scale asset purchases with multiple equilibria');
title(titleStr,'Interpreter','Latex','FontSize',20);
xlabel('Normalized price, $p_0$','Interpreter','Latex','FontSize',20);

legendStr1 = sprintf('Actual Sharpe ratio, $r^f_0=0$');
legendStr2 = sprintf('Required Sharpe ratio without LSAPs');
legendStr3 = sprintf('Required Sharpe ratio with LSAPs, $\\lambda=%1.1f$',lsap);
hL = legend(h([1 3 2]),legendStr1,legendStr3,legendStr2);
set(hL,'orientation','vertical','fontsize',15,'interpreter','latex');
xlim([ph 1]);
ylim([sharpeRequiredArr_g(end) sharpeActualArr(1)]);
saveas(gcf,[pwd '/figures/fig5.eps'],'epsc');


